<template>
  <div id="app">
    <!-- 表格展示 -->
    <el-table :data="books" style="width: 100%" :header-cell-style="{ background: '#409EFF', color: '#FFFFFF' }">
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column label="书名" width="180">
        <template slot-scope="scope">
          <el-link :href="`https://example.com/book/${scope.row.id}`" target="_blank" type="primary">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="作者">
        <template slot-scope="scope">
          <el-link type="success">{{ scope.row.author }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="库存">
        <template slot-scope="scope">
          <el-button type="text" @click="decreaseCopies(scope.row)" style="color: #F56C6C;">
            <i class="el-icon-minus"></i>
          </el-button>
          {{ scope.row.copies }}
          <el-button type="text" @click="increaseCopies(scope.row)" style="color: #67C23A;">
            <i class="el-icon-plus"></i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="deleteBook(scope.row.id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加图书的表单 -->
    <el-form :model="newBook" label-width="80px" style="margin-top: 20px;">
      <el-form-item label="书名">
        <el-input v-model="newBook.title"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="newBook.author"></el-input>
      </el-form-item>
      <el-form-item label="库存">
        <el-input-number v-model="newBook.copies" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addBook">添加图书</el-button>
      </el-form-item>
    </el-form>

    <!-- 底部导航栏 -->
    <van-tabbar route>
      <van-tabbar-item replace to="/home" icon="home-o">首页</van-tabbar-item>
      <van-tabbar-item replace to="/search" icon="search">搜索</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      // 初始化图书数据
      books: [
        { id: 1, title: "Python编程", author: "John Doe", copies: 4 },
        { id: 2, title: "历史的秘密", author: "Jane Smith", copies: 3 },
        { id: 3, title: "宇宙探索", author: "Emily Brown", copies: 2 },
        { id: 4, title: "健康生活", author: "Mike Johnson", copies: 5 },
        { id: 5, title: "数学之美", author: "Sarah Lee", copies: 4 },
        { id: 6, title: "艺术欣赏", author: "David White", copies: 3 },
        { id: 7, title: "编程入门", author: "Alice Green", copies: 2 },
        { id: 8, title: "科幻世界", author: "Frank Black", copies: 5 },
        { id: 9, title: "自然奇观", author: "Grace Blue", copies: 4 },
        { id: 10, title: "音乐之声", author: "Tom Red", copies: 3 }
      ],
      // 新增图书的表单数据
      newBook: {
        title: '',
        author: '',
        copies: 1
      }
    };
  },
  methods: {
    // 添加图书的方法
    addBook() {
      if (!this.newBook.title || !this.newBook.author) {
        this.$message.error('书名和作者不能为空');
        return;
      }
      const newId = this.books.length > 0 ? this.books[this.books.length - 1].id + 1 : 1;
      this.books.push({ ...this.newBook, id: newId });
      this.newBook = { title: '', author: '', copies: 1 }; // 清空表单
      this.$message.success('图书添加成功');
    },
    // 删除图书的方法
    deleteBook(id) {
      this.books = this.books.filter(book => book.id !== id);
      this.$message.success('图书删除成功');
    },
    // 减少库存数量的方法
    decreaseCopies(book) {
      if (book.copies > 0) {
        book.copies -= 1;
      }
    },
    // 增加库存数量的方法
    increaseCopies(book) {
      book.copies += 1;
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

/* 添加表格样式 */
.el-table {
  background-color: #F2F6FC;
}

.el-table th {
  background-color: #409EFF;
  color: #FFFFFF;
}

.el-table td {
  background-color: #FFFFFF;
  color: #303133;
}
</style>